package edu.whu.xterm.http;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import android.content.Context;
import android.util.Log;
import edu.whu.xterm.Article;
import edu.whu.xterm.XApplication;
import edu.whu.xterm.util.Constants;

public class RequestBuilder {

	private static final String TAG = "HLoginRequest";

	private static final String K_USR = "id";
	private static final String K_PWD = "passwd";
	private static final String K_KICK_MULTI = "kick_multi";
	private static final String K_WEB_TYPE = "webtype";

	private static final String BOARD = "board";
	private static final String RE_ID = "reID";
	private static final String FONT = "font";
	private static final String SUBJECT = "subject";
	private static final String CONTENT = "Content";
	private static final String SIGNATURE = "signature";

	HttpClient client;

	private String usr;

	private String pwd;

	private Context mContext;

	private static RequestBuilder builder;

	public static void init(Context ctx) {
		builder = new RequestBuilder(ctx);
	}

	public void setAuth(String usr, String pwd) {
		this.usr = usr;
		this.pwd = pwd;
	}

	private RequestBuilder(Context ctx) {
		mContext = ctx;
		this.client = ((XApplication) ctx).getHClient();
		// this.hContext = new BasicHttpContext();
		// hContext.setAttribute(ClientContext.COOKIE_STORE, new
		// BasicCookieStore());
		builder = this;
	}

	public static RequestBuilder getBuilder() {
		return builder;
	}

	public RequestWorker loginRequest() {
		String url = Constants.SITE + Constants.PATH_LOGIN;
		final HttpPost post = new HttpPost(url);
		List<NameValuePair> list = new ArrayList<NameValuePair>();
		list.add(new BasicNameValuePair(K_USR, usr));
		list.add(new BasicNameValuePair(K_PWD, pwd));
		list.add(new BasicNameValuePair(K_WEB_TYPE, "wforum"));// classic
		try {
			post.setEntity(new UrlEncodedFormEntity(list));
		} catch (UnsupportedEncodingException e) {
			Log.d(TAG, "", e);
		}
		return new RequestWorker(client, post);

	}

	public void logoutRequest() {
		String url = Constants.SITE + Constants.PATH_LOGOUT;
		final HttpGet get = new HttpGet(url);
		try {
			new RequestWorker(client, get).call();
		} catch (Exception e) {
			// do nothing
		}
	}

	public RequestWorker replyRequest(Article preArticle, String title, String content)
			throws UnsupportedEncodingException {
		List<NameValuePair> variables = new ArrayList<NameValuePair>();
		variables.add(new BasicNameValuePair(BOARD, preArticle.board));
		variables.add(new BasicNameValuePair(RE_ID, preArticle.id + ""));
		// variables.add(new BasicNameValuePair(FONT, null));
		variables.add(new BasicNameValuePair(SUBJECT, title));
		variables.add(new BasicNameValuePair(CONTENT, content));
		variables.add(new BasicNameValuePair(SIGNATURE, "1"));
		final HttpPost post = new HttpPost(Constants.SITE + Constants.PATH_DO_POST_ARTICLE_WFORUM);
		post.addHeader(HTTP.CONTENT_ENCODING, "gzip,deflate");
		post.setEntity(new UrlEncodedFormEntity(variables, "gb2312"));
		return new RequestWorker(client, post);
	}

	public RequestWorker kickMultiRequest() throws UnsupportedEncodingException, MalformedURLException {
		String url = Constants.SITE + Constants.PATH_LOGIN;
		final HttpPost post = new HttpPost(url.toString());
		List<NameValuePair> list = new ArrayList<NameValuePair>();

		list.add(new BasicNameValuePair("mainurl", null));
		list.add(new BasicNameValuePair(K_USR, "harry"));
		list.add(new BasicNameValuePair(K_PWD, "nestle123"));
		list.add(new BasicNameValuePair(K_KICK_MULTI, "1"));
		post.setEntity(new UrlEncodedFormEntity(list));
		return new RequestWorker(client, post);
	}

	public DocumentWorker top10RequestWforum() {
		String url = Constants.SITE + Constants.PATH_MAIN_PAGE_WFORUM;
		return new DocumentWorker(mContext, url);
	}

	public DocumentWorker pageRequest(String url) {
		return new DocumentWorker(mContext, url);
	}

	public PageStringWorker loginPageRequest(String url) {
		return new PageStringWorker(client, url);
	}

}
